﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    using DAL.Entities;

    public class CothuocDAL : BaseDAL, UTL.IBaseDAL 
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            throw new NotImplementedException();
        }
        
        public DataTable Select(object obj)
        {
            var o = (cothuoc)obj;
            string sql = @"SELECT *,b.tenthuoc,c.madonthuoc from COTHUOC a join THUOC b on a.mathuoc=b.mathuoc join DONTHUOC c on a.madonthuoc=c.madonthuoc where c.madonthuoc='{0}'";
            sql = string.Format(sql, o.madonthuoc);

            return ExecuteQuery(sql);
            
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            throw new NotImplementedException();
            
        }

        public bool Insert(object obj)
        {
            var o = (cothuoc)obj;
            string sql = @"INSERT INTO cothuoc(mathuoc,madonthuoc,soluong) VALUES('{0}','{1}','{2}')";
            sql = string.Format(sql,o.mathuoc,o.madonthuoc,o.soluong);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (cothuoc)obj;
            string sql = @" UPDATE cothuoc SET soluong='{0}'  WHERE  mathuoc='{1}'and madonthuoc='{2}' ";
            sql = string.Format(sql, o.soluong, o.mathuoc, o.madonthuoc);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }

        public DataTable Select_mathuoc()
        {
            string sql = @"SELECT mathuoc as id, tenthuoc as ten FROM THUOC";
            return ExecuteQuery(sql);
        }

        public DataTable Select_madonthuoc()
        {
            string sql = @" SELECT madonthuoc as id  FROM DONTHUOC";
            return ExecuteQuery(sql);
        }
        /// <summary>
        /// lay ...
        /// </summary>
        /// <returns></returns>
        public string Select_max()
        {
            string sql = @"SELECT max(mathuoc) as id FROM thuoc";
            var tb= ExecuteQuery(sql);
            
            return tb.Rows.Count>0? tb.Rows[0]["id"]+"":"0";
        }



        public bool deleteobj(object obj)
        {
            var o = (cothuoc)obj;
            string sql = @"DELETE FROM cothuoc WHERE mathuoc= '{0}' and madonthuoc='{1}'";
            sql = string.Format(sql, o.mathuoc, o.madonthuoc);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Select_thuoc()
        {
            string sql = @" SELECT mathuoc,tenthuoc FROM thuoc ";
            //sql = string.Format(sql);
            return ExecuteQuery(sql);
        }

        public DataTable select_mathuoctheodonthuoc(string id)
        {
            string sql ="select mathuoc as id,tenthuoc as ten from THUOC where MATHUOC not in (select MATHUOC from COTHUOC where MADONTHUOC='{0}')";
            sql = string.Format(sql, id);
            return ExecuteQuery(sql);
        }
    }
}
